var time = "00:00:00"; 
var timer_id = 0;
let ms = 0, s = 0, m = 0, h = 0;

function timer(){   //定义计时函数
  ms = ms + 50;         //毫秒
  if(ms >= 1000){
      ms = 0;
      s = s +1;         //秒
  }
  if(s>=60){
      s=0;
      m=m+1;        //分钟
  }
  if(m>=60){
      m=0;
      h=h+1;        //小时
  }
  time = toDub(h)+":"+toDub(m)+":"+toDub(s);
}

function toDub(n){  //补0操作
  if(n<10){
      return "0"+n;
  }
  else {
      return ""+n;
  }
}

function start(){
  timer_id = setInterval(function(){timer()}, 50);
}

function end(){
  clearInterval(timer_id);
}

function reset(){
  time = "00:00:00";
  ms = 0, s = 0, m = 0, h = 0;
  timer_id = 0;
}

function getTime(){
  return time;
}

//4.14加入
function getMonth(){
  var date = new Date();
  var month = date.getMonth();

  return month + 1;

}

function getTodayHour(){
  var date = new Date();
  var year = date .getFullYear(); //获取完整的年份(4位)
  var month = date .getMonth(); //获取当前月份(0-11,0代表1月)
  var day = date .getDate(); //获取当前日(1-31)

  return year+"/"+(month + 1 )+"/"+day;
}

function getTodayTime(){
  var date = new Date();
  var year = date .getFullYear(); //获取完整的年份(4位)
  var month = date .getMonth(); //获取当前月份(0-11,0代表1月)
  var day = date .getDate(); //获取当前日(1-31)
  // var mytime=date .toLocaleTimeString();
  var mytime = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
  return year+"/"+(month + 1 )+"/"+day + " " + mytime;
}
function getTmC(star,end){

  var date1 = new Date(star);
  var date2 = new Date(end);

  var s1 = date1.getTime(),s2 = date2.getTime();

  var total = (s2 - s1)/1000;

  var day = parseInt(total / (24*60*60));//计算整数天数
  var afterDay = total - day*24*60*60;//取得算出天数后剩余的秒数
  var hour = parseInt(afterDay/(60*60));//计算整数小时数
  var afterHour = total - day*24*60*60 - hour*60*60;//取得算出小时数后剩余的秒数
  var min = parseInt(afterHour/60);//计算整数分
  var afterMin = total - day*24*60*60 - hour*60*60 - min*60;//取得算出分后剩余的秒数


  
  var getHourAndMin = resultFormat(total, afterMin);
  return getHourAndMin;
}
function resultFormat(result, afterMin) {
  var h = Math.floor(result/3600%24);
  var m = Math.floor(result/60%60);
   if(m === 0 && h < 1 ){
       return result = afterMin + "秒"
   }
  else if (h < 1) {
      return result = m + "分钟" + afterMin + "秒";
  } else {
      return result = h + "小时" + m + "分钟" + "秒";
  }
}
module.exports = {
  start: start,
  end: end,
  reset: reset,
  getTime: getTime,
  getTodayHour:getTodayHour,
  getTodayTime:getTodayTime,
  getTmC:getTmC,
  getMonth:getMonth
}